package com.example.demo_hilt.demo_module;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.TimeUnit;

import javax.inject.Qualifier;
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author yaotianxue
 * @description

    @Module：**用于提供依赖注入实例的模块
    @InstallIn：装载到哪个模块中
    @Provides：提供获取实例的方法
    @Singleton：一般开发者需要将Retorfit定义成单例模式，只需要将@InstallIn属性值设置为SingletonComponent.class，之前api为ApplicaitonComponent.class，并在方法上添加@Singleton注解

 */
@Module
@InstallIn(SingletonComponent.class)
public class RetrofitManagerDemo {
    @Provides
    @Singleton
    @Retrofit1
    public Retrofit getRetrofit(){
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        Retrofit mRetrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl("http://43.143.146.165:8181")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return mRetrofit;
    }


    @Provides
    @Singleton
    @Retrofit2
    public Retrofit getRetrofit2(){
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//                .addInterceptor()
                .build();
        Retrofit mRetrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl("http://43.143.146.165:8484")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return mRetrofit;
    }


    /**
     * 解决2个方法中提供的实例是同一种类型，无法区别的问题
     */
    @Qualifier
    @Retention(RetentionPolicy.CLASS)
    public @interface Retrofit1{

    }
    @Qualifier
    @Retention(RetentionPolicy.CLASS)
    public @interface Retrofit2{

    }

}
